package com.neuedu.mybatis.dao;

import com.neuedu.mybatis.po.Dept;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;

import java.util.ArrayList;
import java.util.List;

/**
 * @author 金山
 * 项目：mybatis-learn
 * site: https://blog.fulfill.com.cn
 * 描述
 * @data 2025/3/510:26
 */
public class DeptDao {

    private SqlSessionFactory sqlSessionFactory;


    public DeptDao(SqlSessionFactory sqlSessionFactory) {
        this.sqlSessionFactory = sqlSessionFactory;
    }

    /**
     * 根据条件查询列表
     */
    public List<Dept> selectList() {

        List<Dept> deptList = new ArrayList<>();
        SqlSession session = null;
        try {
            // 创建session对象    SqlSession  用户接口
            session = sqlSessionFactory.openSession();
            deptList = session.selectList("DeptDao.selectList");

        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (session != null) {
                session.close();
            }
        }

        return deptList;
    }

    /**
     * 根据主键查询
     *
     * @param id
     */
    public Dept selectById(int id) {

        Dept dept = null;
        SqlSession session = null;
        try {
            // 创建session对象    SqlSession  用户接口
            session = sqlSessionFactory.openSession();
            dept = session.selectOne("DeptDao.selectById", id);

        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (session != null) {
                session.close();
            }
        }
        return dept;
    }

    /**
     * 查询总条数
     */
    public long count() {
        long count = 0;
        SqlSession session = null;
        try {
            // 创建session对象    SqlSession  用户接口
            session = sqlSessionFactory.openSession();
            count = session.selectOne("DeptDao.count");

        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (session != null) {
                session.close();
            }
        }

        return count;
    }

    /**
     * 根据主键更新   可以有   updateByCondition
     */
    public int updateById(Dept dept) {
        SqlSession session = null;
        int count = 0;
        try {
            // 创建session对象    SqlSession  用户接口
            session = sqlSessionFactory.openSession();
            // 影响函数
            count = session.update("DeptDao.updateById", dept);
            session.commit();
        } catch (Exception e) {
            session.rollback();
            e.printStackTrace();
        } finally {
            if (session != null) {
                session.close();
            }
        }
        return count;
    }

    /**
     * public int
     *
     * @param dept
     * @return
     */
    public int insert(Dept dept) {
        SqlSession session = null;
        int count = 0;
        try {
            // 创建session对象    SqlSession  用户接口
            session = sqlSessionFactory.openSession();
            // 影响函数
            count = session.insert("DeptDao.insert", dept);
            session.commit();
        } catch (Exception e) {
            session.rollback();
            e.printStackTrace();
        } finally {
            if (session != null) {
                session.close();
            }
        }
        return count;
    }

    /**
     * 删除一条
     *
     * @param id
     * @return
     */
    public int deleteById(int id) {
        SqlSession session = null;
        int count = 0;
        try {
            // 创建session对象    SqlSession  用户接口
            session = sqlSessionFactory.openSession();
            // 影响函数
            count = session.insert("DeptDao.deleteById", id);
            session.commit();
        } catch (Exception e) {
            session.rollback();
            e.printStackTrace();
        } finally {
            if (session != null) {
                session.close();
            }
        }
        return count;
    }


}
